﻿using System;
using System.Threading;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Hardware;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace ServiceDemo
{
    [Service]
    [IntentFilter(new String[] { "ServiceDemo" })]
    public class TestService : Service
    {
        public override IBinder OnBind(Intent intent)
        {
            return new TestServiceBinder(this);
        }

        private SensorManager sensor_manager;
        private GraphView graph_view;
        public override void OnStart(Intent intent, int startId)
        {
            base.OnStart(intent, startId);

        }

        public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
        {
            Log.Debug("TestService", "TestService started");

            StartServiceInForeground();

            return StartCommandResult.NotSticky;
        }

        void StartServiceInForeground()
        {
            //var ongoing = new Notification(Resource.Drawable.Icon, "DemoService in foreground");
            //var pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0);
            //ongoing.SetLatestEventInfo(this, "DemoService", "DemoService is running in the foreground", pendingIntent);

            //StartForeground((int)NotificationFlags.ForegroundService, ongoing);
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
            Log.Debug("TestService", "TestService stopped");
        }

        public string DoSomething()
        {
            return "what are you want to do now?";
        }

        private Thread _syncThread;

        public void ProcessStart()
        {
            //_syncThread = new Thread(Send);
            //_syncThread.Start();
            sensor_manager = (SensorManager)GetSystemService(SensorService);
            graph_view = new GraphView(this);

            var accel = sensor_manager.GetDefaultSensor(SensorType.Accelerometer);
            var mag = sensor_manager.GetDefaultSensor(SensorType.MagneticField);
            var ori = sensor_manager.GetDefaultSensor(SensorType.Orientation);

            sensor_manager.RegisterListener(graph_view, ori, SensorDelay.Fastest);
            sensor_manager.RegisterListener(graph_view, accel, SensorDelay.Fastest);
            sensor_manager.RegisterListener(graph_view, mag, SensorDelay.Fastest);
        }

        public void ProcessStop()
        {
            _syncThread.Abort();
            _syncThread.Join();
        }

        private void Send()
        {
            while (true)
            {
                Intent intent = new Intent(Constants.MessageCode);
                intent.PutExtra(Constants.Key, DateTime.Now.ToString());
                SendBroadcast(intent);
                Thread.Sleep(5000);
            }
        }

        private class GraphView : View, ISensorEventListener
        {
            public GraphView(Context context)
                : base(context)
            {
            }

            protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
            {
                base.OnSizeChanged(w, h, oldw, oldh);
            }


            protected override void OnDraw(Canvas canvas)
            {
            }

            public void OnSensorChanged(SensorEvent e)
            {
                var sensor = e.Sensor;
                var values = e.Values;

                lock (this)
                {
                    foreach (var value in values)
                    {
                        Log.Debug("ServiceDemo", value.ToString());
                    }
                }
            }

            public void OnAccuracyChanged(Sensor sensor, SensorStatus accuracy)
            {
                // Do nothing
            }
        }
    }
}
